package com.oocl.o2o.client.dao.impl;

import java.util.Date;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.oocl.o2o.client.dao.OrderInfoDao;
import com.oocl.o2o.client.entity.OrderInfo;
import com.oocl.o2o.client.entity.OrderStatus;
import com.oocl.o2o.client.util.ConfigBaseUtil;
import com.oocl.o2o.client.util.Configuration;
import com.oocl.o2o.client.util.DateCompareTool;

/**
 * @description: OrderInfoDaoImpl
 * @author  XIEMI2
 * @date Aug 12, 2017 10:03:15 AM
 * @version 1.0
 */
@Repository(value="orderInfoDao")
public class OrderInfoDaoImpl implements OrderInfoDao{

	@PersistenceContext(name="un")
	private EntityManager em;
	
	@Override
	public List<OrderInfo> findAll() {
		String jpql = "from OrderInfo";
		List<OrderInfo> orderInfos = em.createQuery(jpql).getResultList();
		return orderInfos;
	}

	@Override
	public OrderInfo add(OrderInfo orderInfo) {
		em.persist(orderInfo);
		return orderInfo;
	}

	@Override
	public OrderInfo delete(String id) {
		OrderInfo orderInfo = em.getReference(OrderInfo.class, id);
		em.remove(orderInfo);
		return orderInfo;
	}

	@Override
	public OrderInfo update(OrderInfo orderInfo) {
		OrderInfo order = em.find(OrderInfo.class, orderInfo.getId());
		em.clear();
		em.merge(orderInfo);
		return orderInfo;
	}

	@Override
	public OrderInfo findById(String orderInfoId) {
		OrderInfo order = em.find(OrderInfo.class, orderInfoId);
		return order;
	}

	@Override
	public List<OrderInfo> findByCustomerName(String customerName) {
		String jpql = "from OrderInfo o where o.customer.username =:customerName";
		List<OrderInfo> orderInfos = em.createQuery(jpql).setParameter("customerName", customerName).getResultList();
		return orderInfos;
	}

	@Override
	public List<OrderInfo> findByCustomerId(String customerId) {
		String jpql = "select o from OrderInfo o where o.customer.id =:customerId";
		List<OrderInfo> orderInfos = em.createQuery(jpql).setParameter("customerId", customerId).getResultList();
		return orderInfos;
	}

	@Override
	public OrderInfo updateStatusById(String orderInfoId) {
		OrderInfo orderInfo = em.find(OrderInfo.class, orderInfoId);
		em.clear();
		orderInfo.setIsComplaint(1);
		em.merge(orderInfo);
		return orderInfo;
	}

	@Override
	public boolean updateOrderInfoStatus(String orderInfoId) {
		OrderInfo orderInfo = em.find(OrderInfo.class, orderInfoId);
		em.clear();
		orderInfo.setOrderStatus(OrderStatus.COMPLETED);
		em.merge(orderInfo);
		return true;
	}

	@Override
	public void autoCommitInfo() {
		
		String jpql = "select o from OrderInfo o where o.orderStatus ='DELIVERING' order by o.orderDate desc";
		List<OrderInfo> orderInfos = em.createQuery(jpql).setMaxResults(Configuration.setMaxResults).getResultList();
		Date date = new Date();
		if(!orderInfos.isEmpty()){
			for(OrderInfo o: orderInfos){
				System.out.println(o.getOrderDate());
				if(DateCompareTool.compare_date(date, o.getOrderDate(), Configuration.autoCommitSycle) == 1){
					OrderInfo orderInfo = em.find(OrderInfo.class, o.getId());
					em.clear();
					orderInfo.setOrderStatus(OrderStatus.COMPLETED);
					em.merge(orderInfo);
				}
			}
		}
	}

}


